<template>
  <by-dialog
    :model-value="showTouch"
    title="温馨提示"
    :content="`${type === '1' ? '预约' : '联系'}对方需消耗${point}积分`"
    :confirm-text="`立即${type === '1' ? '预约' : '联系'}`"
    @confirm="toRelation"
    @close="$emit('update:showTouch',false)"
  />

  <by-contact-way
    :info="info"
    :model-value="isShowContact"
    @close="$emit('update:showContactWay',false)"
  />

  <by-dialog
    :model-value="showError"
    title="积分不足"
    :content="`只需${point}积分即可${type === '1' ? '预约技师' : '查看微信及电话'}，${userStore.canPay ? '是否前往获取积分?' : '请联系客服获取'}`"
    confirm-text="获取积分"
    @close="$emit('update:showError',false)"
    @confirm="jump(userStore.canPay ? '/pages/my-wallet/index' : '/pages/web-view/index')"
  />

  <by-safeguard-dialog
    :model-value="showSafeguard"
    @close="$emit('update:showSafeguard',false)"
  />

  <by-dialog
    :model-value="showEvaluate"
    :show-cancel="!commentValue"
    :confirm-text="!!commentValue ? '知道了' : '确定'"
    @close="$emit('update:showEvaluate', false)"
    @confirm="handleAddComment">
    <view class="padding-lr-40 padding-top-30 padding-bottom-10">
        <by-score :disabled="!!commentValue" v-model="score" />
    </view>
  </by-dialog>
</template>

<script setup lang="ts">
import useUserStore from '@/store/user'
import { jump, toast } from '@/common/util'
import { unlockContact, showContact, addComment } from '@/common/api'
import type { AnyObject, ICommentInfo } from '@/common/types'

const userStore = useUserStore()

interface IProps{
  showTouch: boolean
  showContactWay: boolean
  showError: boolean
  showSafeguard: boolean
  showEvaluate: boolean
  type?: string // 1:预约  default: 联系
  point?:number
  commentValue?:number
  tno?:string
  commentInfo?:ICommentInfo
}

const props = withDefaults(defineProps<IProps>(), {
  showTouch: false,
  showContactWay: false,
  showError: false,
  showSafeguard: false,
  showEvaluate: false
})

const $emit = defineEmits(['update:showTouch', 'update:showContactWay', 'update:showError', 'update:showSafeguard', 'update:showEvaluate', 'showContact', 'reload', 'submitSubscribe'])

const score = ref(0)
watch(
  () => props.showEvaluate,
  (e) => {
    if (!e) {
      setTimeout(() => {
        score.value = 0
      }, 300)
    }
  }
)

watch(
  () => props.showContactWay,
  (e) => {
    if (e) handleShowContact()
    else isShowContact.value = e
  }
)

watch(
  () => props.commentValue,
  (e) => {
    if (e) score.value = e
  }
)

async function toRelation() {
  if (props.type === '1') {
    $emit('submitSubscribe')
  } else {
    const res = await unlockContact(props.tno as string)
    $emit('update:showTouch', false)
    if (res.rspCode === '3001') $emit('update:showError', true)
    else $emit('showContact')
  }
}

const isShowContact = ref(false)
const info = shallowRef<AnyObject>({})
async function handleShowContact() {
  const res = await showContact(props.tno as string)
  const { data } = res
  if (data.unlock) {
    info.value = {
      contactTel: data.contactTel,
      wxNo: data.wxNo
    }
    isShowContact.value = true
  }
}

async function handleAddComment() {
  if (!props.commentValue && score.value) {
    await addComment(Object.assign({
      star: score.value
    }, props.commentInfo))
    toast('评价成功')
    $emit('reload')
  }
}
</script>

<style scoped lang="scss">

</style>
